package com.outstudio.plan.main;

import java.io.IOException;
import java.util.List;

import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;

import com.itextpdf.text.DocumentException;
import com.outstudio.plan.op.pdf.PDFPageCreater;
import com.outstudio.plan.op.pdf.model.HomePage;
import com.outstudio.plan.op.pdf.model.Room;
import com.outstudio.plan.op.svg.JsonOp;
import com.outstudio.plan.op.svg.PrimaryJsonPaser;
import com.outstudio.plan.op.svg.SvgOp;
import com.outstudio.plan.op.svg.SvgUtil;
import com.outstudio.plan.util.FileOp;

public class Svg2Pdf {

	public static void main(String[] args) {
		String tempDir = "E:/svgTemp";
		FileOp.createTempDir(tempDir);

		// 设置在目标目录下存放额外txt的文件夹
		PrimaryJsonPaser.setExtDirName("/ext/");
		// 转化json文件为doc
		Document doc = JsonOp.trans("A:/TestData/svg/svg20160619.txt", "A:/TestData/svg");

		try {
			System.out.println("svg format finish!");
			SvgUtil.printSvg(doc);
		} catch (TransformerException e1) {
			e1.printStackTrace();
		}

		// 生成pdfCreater
		PDFPageCreater ppc = new PDFPageCreater();
		// 打开doc流
		ppc.start("E:/", "pdf.pdf");

		// 将doc中内容提取为pdf的页对象
		SvgOp so = new SvgOp(tempDir);
		HomePage hp = so.getHomePage(doc);
		List<Room> rooms = so.getRoom(doc);
		// 将每一页的对象赋到pdf的每一页中
		try {
			ppc.createMainPage(hp);
			for (Room room : rooms) {
				ppc.createNewPage(room);
			}
			ppc.finish();
		} catch (DocumentException | IOException e) {
			e.printStackTrace();
		}

		FileOp.deleteDir(tempDir);
	}

}
